﻿Imports Aricie.DNN.Services.Url.FriendlyUrl.Controls.GroupEntities
Imports DotNetNuke.Services.Localization

Namespace Aricie.DNN.Services.Url.FriendlyUrl.Controls.GroupManagement


    Public MustInherit Class BaseUrlGroupManager(Of T As UrlGroup)
        Implements IFUPGroupManager

        Public Function GetTypeName() As String Implements IFUPGroupManager.GetTypeName
            Return GetType(T).Name
        End Function

        Public Function fromUIGroup(UIG As GroupEntities.UIGroup) As UrlGroup Implements IFUPGroupManager.fromUIGroup
            Return fromUIGroupInternal(UIG)
        End Function

        Public Function toUIGroup(ug As UrlGroup) As GroupEntities.UIGroup Implements IFUPGroupManager.toUIGroup
            Dim UIG As UIGroup = toUIGroupInternal(DirectCast(ug, T))
            UIG.Type = GetTypeName()
            Return UIG
        End Function

        Public MustOverride Function toUIGroupInternal(ug As T) As UIGroup
        Public MustOverride Function fromUIGroupInternal(UIG As UIGroup) As T
    End Class

    Public Class ProviderUrlGroupManager
        Inherits BaseUrlGroupManager(Of ProviderUrlGroup)


        Public Overrides Function fromUIGroupInternal(UIG As GroupEntities.UIGroup) As ProviderUrlGroup
            Return New ProviderUrlGroup() With {.UrlProviderName = UIG.ExtendedType, .GroupName = UIG.Name}
        End Function

        Public Overrides Function toUIGroupInternal(ByVal ug As ProviderUrlGroup) As GroupEntities.UIGroup
            Dim ugTooltip As String = String.Empty
            If ug.GroupParamRewrite IsNot Nothing Then
                ugTooltip = ug.GroupParamRewrite.Description
            End If

            Return New UIGroup() With {.ExtendedType = ug.UrlProviderName, .Name = ug.FriendlyName, .Tooltip = ugTooltip}
        End Function
    End Class

    Public Class UrlPartGroupManager
        Inherits BaseUrlGroupManager(Of UrlPartGroup)

        Public Overrides Function fromUIGroupInternal(UIG As GroupEntities.UIGroup) As UrlPartGroup
            Return New UrlPartGroup() With {.UrlPartName = Common.GetEnum(Of UrlPart)(UIG.Name)}
        End Function

        Public Overrides Function toUIGroupInternal(ug As UrlPartGroup) As GroupEntities.UIGroup
            Return New UIGroup() With {.Name = ug.FriendlyName, .Tooltip = Localization.GetString("UrlPart." & ug.UrlPartName.ToString(), FriendlierUrlContext.SharedResourceFile)} '"Url Part " & ug.UrlPartName.ToString} 'ug.Decription}
        End Function
    End Class

    Public Class QueryStringUrlGroupManager
        Inherits BaseUrlGroupManager(Of QueryStringUrlGroup)

        Public Overrides Function fromUIGroupInternal(UIG As GroupEntities.UIGroup) As QueryStringUrlGroup
            Return New QueryStringUrlGroup()
        End Function

        Public Overrides Function toUIGroupInternal(ug As QueryStringUrlGroup) As GroupEntities.UIGroup
            Return New UIGroup() With {.Name = DirectCast(IIf(Not String.IsNullOrEmpty(ug.FriendlyName), ug.FriendlyName, Localization.GetString("QueryStringGroup.Name", FriendlierUrlContext.SharedResourceFile)), String), .Tooltip = Localization.GetString("QueryStringGroup.Description", FriendlierUrlContext.SharedResourceFile)} ' ug.Decription}
        End Function
    End Class

    Public Class AliasUrlGroupManager
        Inherits BaseUrlGroupManager(Of AliasUrlGroup)

        Public Overrides Function fromUIGroupInternal(UIG As GroupEntities.UIGroup) As AliasUrlGroup
            ' filtering disabled on the alias groups
            Return New AliasUrlGroup() With {.AliasName = UIG.ExtendedType, .EnableFilter = False} ' .Name = UIG.Name, .Decription = UIG.Tooltip }
        End Function

        Public Overrides Function toUIGroupInternal(ug As AliasUrlGroup) As GroupEntities.UIGroup
            Return New UIGroup With {.Name = ug.AliasName, .ExtendedType = ug.AliasName, .Tooltip = String.Format(Localization.GetString("Alias.Description", FriendlierUrlContext.SharedResourceFile), ug.AliasName, ug.AliasRule.Instances.Count)}
        End Function
    End Class

    Public Class ConstantUrlGroupManager
        Inherits BaseUrlGroupManager(Of ConstantUrlGroup)

        Public Overrides Function fromUIGroupInternal(UIG As GroupEntities.UIGroup) As ConstantUrlGroup
            Return New ConstantUrlGroup()
        End Function

        Public Overrides Function toUIGroupInternal(ug As ConstantUrlGroup) As GroupEntities.UIGroup
            Return New UIGroup With {.Name = DirectCast(IIf(Not String.IsNullOrEmpty(ug.FriendlyName), ug.FriendlyName, Localization.GetString("ConstantUrlGroup.Name", FriendlierUrlContext.SharedResourceFile)), String), .Tooltip = Localization.GetString("ConstantUrlGroup.Description", FriendlierUrlContext.SharedResourceFile)} 'ug.Decription}
        End Function
    End Class

End Namespace